package com.day01;

import java.sql.*;
import java.util.Iterator;

public class MysqlIterator implements Iterator<UserInfo> {
    private ResultSet resultSet = null;
    private UserInfo user = new UserInfo();

    public  MysqlIterator(){
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "123456");
            PreparedStatement pps = conn.prepareStatement("select * from user_info");
            resultSet = pps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //判断还有没有数据
    @Override
    public boolean hasNext() {
        boolean next = true;
        try {
            next = resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return next;
    }

    //获取数据
    @Override
    public UserInfo next() {
        try {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            String gender = resultSet.getString("gender");
            user.setAge(age);
            user.setGender(gender);
            user.setName(name);
            user.setId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}
